from fastapi import FastAPI
from langchain.prompts import ChatPromptTemplate
from langserve import add_routes

from com.wp.langchain.exercise.LLMInit import LLMInit

app = FastAPI(
    title="LangChain Server",
    version="1.0",
    description="A simple api server using Langchain's Runnable interfaces",
)

# 添加路由到应用中
add_routes(
    app,
    LLMInit().get_llm(),
    path="/anthropic",
)

# 创建一个讲笑话的模板
model = LLMInit().get_llm()
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
add_routes(
    app,
    prompt | model,
    path="/joke",
)

#如果你直接运行这个Python脚本，它会启动一个服务器
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="localhost", port=8000)
